package org.spring.security.oauth2.auth.server;


import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;





@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

        @Autowired
        AuthenticationManager authenticationManager;

        /*
        @Autowired
        RedisConnectionFactory redisConnectionFactory;
         */

        @Autowired
        DataSource dataSource;
        
        @Bean("clientPasswordEncoder")
        PasswordEncoder clientPasswordEncoder() {
            return new BCryptPasswordEncoder(8);
        }
        
        @Bean
        public TokenStore tokenStore() {
            return new JdbcTokenStore(dataSource);
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {

        	// the password must encrypt or it will be error
            // password 方案三：支持多种编码，通过密码的前缀区分编码方式
            //String finalSecret = "{bcrypt}"+new BCryptPasswordEncoder().encode("123456");
            //配置两个客户端,一个用于password认证一个用于client认证
/*            clients.inMemory().withClient("client_1")
                    .resourceIds("app")
                    .authorizedGrantTypes("client_credentials", "refresh_token")
                    .scopes("select")
                    .authorities("oauth2")
                    .secret(finalSecret)
                   ;*/
            
            clients.jdbc(dataSource);
        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
            endpoints
                    //.tokenStore(new RedisTokenStore(redisConnectionFactory))
                    .tokenStore(tokenStore())
                    .authenticationManager(authenticationManager)
                    .allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST);
        }

        
        @Override
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
            //oauthServer.allowFormAuthenticationForClients();
        	
            // This will enable /oauth/check_token access
        	oauthServer.checkTokenAccess("permitAll");

            // BCryptPasswordEncoder(8) is used for oauth_client_details.user_secret
        	oauthServer.passwordEncoder(clientPasswordEncoder());
        	
            //允许表单认证
            oauthServer.allowFormAuthenticationForClients();
        }

}


